package main;

import com.kcbsdk.AFetchType;
import com.kcbsdk.CSvrDomain;
import com.kcbsdk.EventHandler;
import com.kcbsdk.MyDomain.ADomainUser;
import com.kcbsdk.MyDomain.AListPage;
import com.kcbsdk.cjmkcs;
import com.kcbsdk.wpapper;
import com.kcbsdk.MyDomain.AAnaDomain;
import com.kcbsdk.MyDomain.ATopDomain;

import android.app.Activity;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by Administrator on 2017/4/5.
 */

public class MyDomain implements wpapper {
	public CSvrDomain mSvrDomain;
	public Activity mActivity = null;
	public EventHandler mEventHandler = null;

	public ATopDomain mATopDomain[];
	public AAnaDomain mAAnaDomain[];

	public ADomainUser mADomainUser[];
	public int g4DbsDomainUserMax;

	public String gsPreUserNeedPrice;
	public String gsNewEffectiveDate;
	public int g4CurrentPrince;

	public String gsRtcText;
	public int g4IPAddress;

	public MyDomain(){
		mSvrDomain = new CSvrDomain();
		mATopDomain = new ATopDomain[5];
	}

	public void pollingTimer()	{
		int methodType;
		for (int counter = 0; counter < 8; counter++){
			methodType = mSvrDomain.getMethodType();
			if (methodType > 0){
				String message = mSvrDomain.getMethodMessage(methodType);
				boolean isFail = ("ok".equals(message)) ? false : true;
				MSTRING(String.format("MyDomain methodType %08X_%s",methodType,message));
				if (!isFail){
                    if ((methodType & tFETCH_DBS_LIST) > 0){
						int retMax[] = new int [1];
                        switch (methodType & 0x000000ff){
                            case TOOLS_DBS_LIST_DDNS:
								mADomainUser = mSvrDomain.methodDomainUserDbs(methodType, retMax);
								g4DbsDomainUserMax = retMax[0];
                                break;
                        }
                    }
                    else {
                    }
				}
				mEventHandler.onComplete(methodType, isFail, message);
			} else{
				break;
			}
		}
		int newEvent = mSvrDomain.pollingEvent();
		if (newEvent > 0){
			if ((newEvent & DOMAIN_EVENT_TOP_DOMAIN) > 0) {
				mATopDomain = mSvrDomain.eventTopDomainDbs();
			}
			if ((newEvent & DOMAIN_EVENT_DBS_ANALYZE) > 0) {
				mAAnaDomain = mSvrDomain.eventAnaDomainDbs();
			}
			if ((newEvent & DOMAIN_EVENT_IP_ARRDRESS) > 0) {
				g4IPAddress = mSvrDomain.eventIPAddress();
			}
//			if ((newEvent & DOMAIN_EVENT_RTC_TEXT) > 0) {
//				gsRtcText = mSvrDomain.eventRtcText();
//			}

			mEventHandler.onComplete(0x00000000, false, "ok");
		}
	}
	public void Log(String text){
		Log.d("Domain", text);
	}
	public void Toast(String text)	{
		Toast.makeText(mActivity, text, Toast.LENGTH_LONG).show();
		Log.d("Domain", text);
	}
	public void setUiEventCallBack(Activity activity, EventHandler handler){
		mActivity = activity;
		mSvrDomain.setMethodType();
		mEventHandler = handler;
		MREG.g4LastSelect = tDOMAIN;
	}



	////////////////////////////////////////////////////////////////////////////////////////////////
	public void fetchTopDomainDbs() {
		mSvrDomain.g4EventMethod |= DOMAIN_EVENT_TOP_DOMAIN;
	}
	public boolean isEventTopDomainDbs()	{
		if ((mSvrDomain.g4EventMethod & DOMAIN_EVENT_TOP_DOMAIN) > 0){
			mSvrDomain.g4EventMethod &= ~DOMAIN_EVENT_TOP_DOMAIN;
			return true;
		}
		return false;
	}

	public void fetchDomainUserList(AFetchType aFetchType) {
		int readType = aFetchType.ordinal();
		mSvrDomain.fetchDomainUserList(readType);
	}
	public boolean isSucessDomainUserDbs(int methodType){
		if ((methodType & 0x000000ff) == TOOLS_DBS_LIST_DDNS){
			return true;
		}
		return false;
	}
	// 域名解析列表方法 ///////////////////////////////////////////////////////////////////////////////////
	public void fetchAnaDomainDbs() {
		mSvrDomain.g4EventMethod |= DOMAIN_EVENT_DBS_ANALYZE;
	}
	public void addAnaDomainDbs(String domainName, String password){	// 增加一个域名到列表
		mSvrDomain.setAnaDomainDbs(0, domainName, password);
	}
	public void deleteAnaDomain(String domainName){	// 删除列表
		mSvrDomain.setAnaDomainDbs(1, domainName, "");
	}
	public void setAnaDomainDevice(String domainName, String password, boolean setTrue){	// 设置为电器域名
		mSvrDomain.setAnaDomainDbs(setTrue ? 3 : 2, domainName, password);
	}
	public void setAnaDomainDirect(String domainName, String password, boolean setTrue){ 	// 设置为直连域名
		mSvrDomain.setAnaDomainDbs(setTrue ? 5 : 4, domainName, password);
	}
	public boolean isEventAnaDomainDbs()	{
		if ((mSvrDomain.g4EventMethod & DOMAIN_EVENT_DBS_ANALYZE) > 0){
			mSvrDomain.g4EventMethod &= ~DOMAIN_EVENT_DBS_ANALYZE;
			return true;
		}
		return false;
	}
	public boolean isEventIPAddress()	{
		if ((mSvrDomain.g4EventMethod & DOMAIN_EVENT_IP_ARRDRESS) > 0){
			mSvrDomain.g4EventMethod &= ~DOMAIN_EVENT_IP_ARRDRESS;
			return true;
		}
		return false;
	}

	////////////////////////////////////////////////////////////////////////////////////////////////
	public void modifyDomainUser(ADomainUser aDomainUser){
		String fullName = aDomainUser.Name + aDomainUser.aTopDomain.Name;
		String password = aDomainUser.ParsePassword;
		String urlRedirect = aDomainUser.RedirectURL;
		String urlRecord = aDomainUser.SiteReference;
		mSvrDomain.modifyDomainUser(fullName, password, aDomainUser.ParseInterval, urlRedirect, urlRecord);
	}
	public void checkAvailableName(String name) {
		mSvrDomain.checkAvailableName(name);
	}
	public void addNewDomainUser(ADomainUser aDomainUser, String authCode){
		String fullName = aDomainUser.Name + "." + aDomainUser.aTopDomain.Name;
		String password = aDomainUser.ParsePassword;
		String urlRedirect = aDomainUser.RedirectURL;
		String urlRecord = aDomainUser.SiteReference;
		mSvrDomain.addNewDomainUser(fullName, password, aDomainUser.ParseInterval, urlRedirect, urlRecord, authCode);
	}
	public void fetchPreUserNeedPrice(ADomainUser aDomainUser, int month){
		String fullName = aDomainUser.Name + "." + aDomainUser.aTopDomain.Name;
		mSvrDomain.fetchPreUserNeedPrice(fullName, aDomainUser.ParseInterval, month);
	}
	public void renewalDomainUser(ADomainUser aDomainUser, int renewTime) {
		String fullName = aDomainUser.Name + "." + aDomainUser.aTopDomain.Name;
		mSvrDomain.fetchPreUserNeedPrice(fullName, aDomainUser.ParseInterval, renewTime);
	}
	public boolean isDnsSupportRedirect(ADomainUser aDomainUser) {
		String fullName = aDomainUser.Name + "." + aDomainUser.aTopDomain.Name;
		return mSvrDomain.isDnsSupportRedirect(fullName);
	}

	////////////////////////////////////////////////////////////////////////////////////////////////
	public boolean isSucessModifyDomainUser(int methodType){
		if ((methodType & 0xffff0000) == DOMAIN_MODIFY_USER){
			return true;
		}
		return false;
	}
	public boolean isSucessCheckAvailableName(int methodType){
		if ((methodType & 0xffff0000) == DOMAIN_IS_AVAILABLE){
			return true;
		}
		return false;
	}
	public boolean isSucessAddNewDomainUser(int methodType){
		if ((methodType & 0xffff0000) == DOMAIN_ADD_NEW_USER){
			return true;
		}
		return false;
	}
	public boolean isSucessFetchPreUserNeedPrice(int methodType){
		if ((methodType & 0xffff0000) == DOMAIN_READ_PRICE){
			gsPreUserNeedPrice = cjmkcs.systemGetWpapperString(wpDOMAIN_PreUserNeedPrice);
			gsNewEffectiveDate = cjmkcs.systemGetWpapperString(wpDOMAIN_NewEffectiveDate);
			g4CurrentPrince = cjmkcs.systemGetWpapperValue(wpDOMAIN_CurrentPrince);
			return true;
		}
		return false;
	}
	public boolean isSucessRenewalDomainUser(int methodType){
		if ((methodType & 0xffff0000) == DOMAIN_USER_RENEWAL){
			return true;
		}
		return false;
	}
	////////////////////////////////////////////////////////////////////////////////////////////////
	public boolean isEventOnDestroy(){
		if ((mSvrDomain.g4EventMethod & DOMAIN_EVENT_ON_DESTROY) > 0){
			return true;
		}
		return false;
	}
	public boolean isEventRtcText()	{
		if ((mSvrDomain.g4EventMethod & DOMAIN_EVENT_RTC_TEXT) > 0){
			mSvrDomain.g4EventMethod &= ~DOMAIN_EVENT_RTC_TEXT;
			return true;
		}
		return false;
	}



	public void MSTRING(String text){
		Log.d("MSYS", text);
	}
}
